function init(){
	var errorMsgList = document.getElementsByClassName("errorMsg");
	for(i=0; i<errorMsgList.length; i++){
		errorMsgList.item(i).innerHTML = "";
	}
}

function validate_required(value){
  if (value==null||value==""){
    return false;
  }
  return true;
}

function validate_length_max(value, length){
	if (value.length > length){
		return false;
	}
	return true;
}

function validate_length_equ(value, length){
	if (value.length != length){
		return false;
	}
	return true;
}

function validate_email_format(email){
	var pattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if (email.search(pattern) == -1)
		return false;
	else 
		return true;
}

function validate_double_number_format(number){
	var pattern = /^([-]?)([0-9]+)((.[0-9])?)$/;
	if (number.search(pattern) == -1)
		return false;
	else 
		return true;
}

function validate_credit_card_format(creditCard){
	alert(creditCard.search(/^\s*\d+\s*$/));
	if (creditCard.search(/^\s*\d+\s*$/) == -1)	
		return false;
	else
		return true;
}


//VALIDATE USER REGISTRATION FORM
function validate_registration_form(){
	var result = true;
	init();
	//CHECK FIRST NAME
	var firstNameLength = 45;
	var firstName = document.getElementsByName("firstName").item(0).value;
	if (!validate_required(firstName)){
		document.getElementById("firstNameErrorMsg").innerHTML=" First name is required!";
		result =  false;
	}else if (!validate_length_max(firstName, firstNameLength)){
		document.getElementById("firstNameErrorMsg").innerHTML=" First name must <= " + length + " !";
		result =  false;
	}
	
	//CHECK LAST NAME
	var lastName = document.getElementsByName("lastName").item(0).value;
	var lastNameLength = 45;
	if (!validate_required(lastName)){
		document.getElementById("lastNameErrorMsg").innerHTML=" Last name is required!";
		result =  false;
	}else if (!validate_length_max(lastName, lastNameLength)){
		document.getElementById("lastNameErrorMsg").innerHTML=" Last name must <= " + length + " !";
		result =  false;
	}
	
	//CHECK EMAIL
	var eMail = document.getElementsByName("eMail").item(0).value;
	if (!validate_required(eMail)){
		document.getElementById("eMailErrorMsg").innerHTML=" E-Mail is required!";
		result =  false;
	}else if (!validate_email_format(eMail)){
		document.getElementById("eMailErrorMsg").innerHTML=" E-Mail format incorrect!";
		result =  false;
	}
	
	//CHECK PASSWORD
	var password = document.getElementsByName("password").item(0).value;
	if (!validate_required(password)){
		document.getElementById("passwordErrorMsg").innerHTML=" Password is required!";
		result =  false;
	}
	
	//CHECK RE-PASSWORD
	var rePassword = document.getElementsByName("rePassword").item(0).value;
	if (!validate_required(rePassword)){
		document.getElementById("rePasswordErrorMsg").innerHTML=" Confirm Password is required!";
		result =  false;
	}else if (!(password == rePassword)){
		document.getElementById("rePasswordErrorMsg").innerHTML=" Confirm Password is equal to Password!";
		result =  false;
	}
	
	//CHECK CREDIT CARD NUMBER
	var creditCardNo = document.getElementsByName("creditCardNo").item(0).value;
	var creditCardLength = 16;
	if (!validate_required(creditCardNo)){
		document.getElementById("creditCardNoErrorMsg").innerHTML=" Credit Card Number is required!";
		result =  false;
	}else if (!validate_length_equ(creditCardNo, creditCardLength)){
		document.getElementById("creditCardNoErrorMsg").innerHTML=" Credit Card Number length must be " + creditCardLength + " !";
		result =  false;
	}else if (!validate_credit_card_format(creditCardNo)){
		document.getElementById("creditCardNoErrorMsg").innerHTML=" Credit Card Number Format incorrect!";
		result =  false;
	}
	
	//CHECK CREDIT CARD EXPIRY
	var cardExpiry = document.getElementsByName("cardExpiry").item(0).value;
	if (!validate_required(cardExpiry)){
		document.getElementById("cardExpiryErrorMsg").innerHTML=" Credit Card Expiry Date is required!";
		result =  false;
	}
	
	if (result)
		return true;
	else
		return false;
}

//VALIDATION PRODUCT ADDITION FORM
function validate_product_addition_form(){
	var result = true;
	init();
	//CHECK PRODUCT NAME
	var productNameLength = 45;
	var productName = document.getElementsByName("productName").item(0).value;
	if (!validate_required(productName)){
		document.getElementById("productNameErrorMsg").innerHTML=" Product name is required!";
		result =  false;
	}else if (!validate_length_max(productName, productNameLength)){
		document.getElementById("productNameErrorMsg").innerHTML=" Product name must <= " + length + " !";
		result =  false;
	}
	
	//CHECK CATEGORY
	var categoryLength = 45;
	var category = document.getElementsByName("category").item(0).value;
	if (!validate_required(category)){
		document.getElementById("categoryErrorMsg").innerHTML=" Category is required!";
		result =  false;
	}else if (!validate_length_max(category, categoryLength)){
		document.getElementById("categoryErrorMsg").innerHTML=" Category must <= " + length + " !";
		result =  false;
	}
	
	//CHECK BRAND
	var brandLength = 45;
	var brand = document.getElementsByName("brand").item(0).value;
	if (!validate_required(brand)){
		document.getElementById("brandErrorMsg").innerHTML=" Brand is required!";
		result =  false;
	}else if (!validate_length_max(brand, brandLength)){
		document.getElementById("brandErrorMsg").innerHTML=" Brand must <= " + length + " !";
		result =  false;
	}
	
	//CHECK SUPPLIER
	var supplierLength = 45;
	var supplier = document.getElementsByName("supplier").item(0).value;
	if (!validate_required(supplier)){
		document.getElementById("supplierErrorMsg").innerHTML=" Supplier is required!";
		result =  false;
	}else if (!validate_length_max(supplier, supplierLength)){
		document.getElementById("supplierErrorMsg").innerHTML=" Supplier must <= " + length + " !";
		result =  false;
	}
	
	//CHECK SPEC1
	var spec1Length = 45;
	var spec1 = document.getElementsByName("spec1").item(0).value;
	if (!validate_required(spec1)){
		document.getElementById("spec1ErrorMsg").innerHTML=" Spec1 is required!";
		result =  false;
	}else if (!validate_length_max(spec1, spec1Length)){
		document.getElementById("spec1ErrorMsg").innerHTML=" Spec1 must <= " + length + " !";
		result =  false;
	}
	
	//CHECK SPEC2
	var spec2Length = 45;
	var spec2 = document.getElementsByName("spec2").item(0).value;
	if (!validate_length_max(spec2, spec2Length)){
		document.getElementById("spec2ErrorMsg").innerHTML=" Spec2 must <= " + length + " !";
		result =  false;
	}
	
	//CHECK SPEC3
	var spec3Length = 45;
	var spec3 = document.getElementsByName("spec3").item(0).value;
	if (!validate_length_max(spec3, spec3Length)){
		document.getElementById("spec3ErrorMsg").innerHTML=" Spec3 must <= " + length + " !";
		result =  false;
	}else if (validate_required(spec3) && !validate_required(spec2)){
		document.getElementById("spec3ErrorMsg").innerHTML=" Please input in order !";
		result = false;
	}
	
	//CHECK SPEC4
	var spec4Length = 45;
	var spec4 = document.getElementsByName("spec4").item(0).value;
	if (!validate_length_max(spec4, spec4Length)){
		document.getElementById("spec4ErrorMsg").innerHTML=" Spec4 must <= " + length + " !";
		result =  false;
	}else if (validate_required(spec4) && !validate_required(spec3)){
		document.getElementById("spec4ErrorMsg").innerHTML=" Please input in order !";
		result = false;
	}
	
	//CHECK SPEC5
	var spec5Length = 45;
	var spec5 = document.getElementsByName("spec5").item(0).value;
	if (!validate_length_max(spec5, spec5Length)){
		document.getElementById("spec5ErrorMsg").innerHTML=" Spec5 must <= " + length + " !";
		result =  false;
	}else if (validate_required(spec5) && !validate_required(spec4)){
		document.getElementById("spec5ErrorMsg").innerHTML=" Please input in order !";
		result = false;
	}
	
	//CHECK SPEC6
	var spec6Length = 45;
	var spec6 = document.getElementsByName("spec6").item(0).value;
	if (!validate_length_max(spec6, spec6Length)){
		document.getElementById("spec6ErrorMsg").innerHTML=" Spec6 must <= " + length + " !";
		result =  false;
	}else if (validate_required(spec6) && !validate_required(spec5)){
		document.getElementById("spec6ErrorMsg").innerHTML=" Please input in order !";
		result = false;
	}
	
	//CHECK SPEC7
	var spec7Length = 45;
	var spec7 = document.getElementsByName("spec7").item(0).value;
	if (!validate_length_max(spec7, spec7Length)){
		document.getElementById("spec7ErrorMsg").innerHTML=" Spec7 must <= " + length + " !";
		result =  false;
	}else if (validate_required(spec7) && !validate_required(spec6)){
		document.getElementById("spec7ErrorMsg").innerHTML=" Please input in order !";
		result = false;
	}
	
	//CHECK SPEC8
	var spec8Length = 45;
	var spec8 = document.getElementsByName("spec8").item(0).value;
	if (!validate_length_max(spec8, spec8Length)){
		document.getElementById("spec8ErrorMsg").innerHTML=" Spec8 must <= " + length + " !";
		result =  false;
	}else if (validate_required(spec8) && !validate_required(spec7)){
		document.getElementById("spec8ErrorMsg").innerHTML=" Please input in order !";
		result = false;
	}
	
	//CHECK SPEC9
	var spec9Length = 45;
	var spec9 = document.getElementsByName("spec9").item(0).value;
	if (!validate_length_max(spec9, spec9Length)){
		document.getElementById("spec9ErrorMsg").innerHTML=" Spec9 must <= " + length + " !";
		result =  false;
	}else if (validate_required(spec9) && !validate_required(spec8)){
		document.getElementById("spec9ErrorMsg").innerHTML=" Please input in order !";
		result = false;
	}
	
	//CHECK SPEC10
	var spec10Length = 45;
	var spec10 = document.getElementsByName("spec10").item(0).value;
	if (!validate_length_max(spec10, spec10Length)){
		document.getElementById("spec10ErrorMsg").innerHTML=" Spec10 must <= " + length + " !";
		result =  false;
	}else if (validate_required(spec10) && !validate_required(spec9)){
		document.getElementById("spec10ErrorMsg").innerHTML=" Please input in order !";
		result = false;
	}
	
	//CHECK DESC
	var descLength = 45;
	var desc = document.getElementsByName("desc").item(0).value;
	if (!validate_length_max(desc, descLength)){
		document.getElementById("descErrorMsg").innerHTML=" Description must <= " + length + " !";
		result =  false;
	}
	
	//CHECK PRICE
	var priceMax = 999999999;
	var price = document.getElementsByName("price").item(0).value;
	if (!validate_required(price)){
		document.getElementById("priceErrorMsg").innerHTML=" Price is required!";
		result =  false;
	}else if (!validate_double_number_format(price)){
		document.getElementById("priceErrorMsg").innerHTML=" Price Format incorrect!";
		result =  false;
	}else if (parseFloat(price) < parseFloat(0)){
		document.getElementById("priceErrorMsg").innerHTML=" Price must > 0!";
		result =  false;
	}else if (parseFloat(price) > parseFloat(priceMax)){
		document.getElementById("priceErrorMsg").innerHTML=" Price over limit " + priceMax + " !";
		result =  false;
	}
	
	//CHECK DISCOUNT
	var discountMax = 100;
	var discount = document.getElementsByName("discount").item(0).value;
	if (!validate_required(discount)){
		document.getElementById("discountErrorMsg").innerHTML=" Discount is required!";
		result =  false;
	}else if (!validate_double_number_format(discount)){
		document.getElementById("discountErrorMsg").innerHTML=" Discount Format incorrect!";
		result =  false;
	}else if (parseFloat(discount) < parseFloat(0)){
		document.getElementById("discountErrorMsg").innerHTML=" Discount must > 0 !";
		result =  false;
	}else if (parseFloat(discount) > parseFloat(discountMax)){
		document.getElementById("discountErrorMsg").innerHTML=" Discount over limit " + discountMax + " !";
		result =  false;
	}
	
	//CHECK STOCK
	var stockMax = 999999999;
	var stock = document.getElementsByName("stock").item(0).value;
	if (!validate_required(stock)){
		document.getElementById("stockErrorMsg").innerHTML=" Stock is required!";
		result =  false;
	}else if (!validate_credit_card_format(stock)){
		document.getElementById("stockErrorMsg").innerHTML=" Stock Format incorrect!";
		result =  false;
	}else if (parseFloat(stock) < parseFloat(0)){
		document.getElementById("stockErrorMsg").innerHTML=" Stock must > 0 !";
		result =  false;
	}else if (parseFloat(stock) > parseFloat(stockMax)){
		document.getElementById("stockErrorMsg").innerHTML=" Stock over limit " + stockMax + " !";
		result =  false;
	}
	
	//CHECK SOLD
	var soldMax = 999999999;
	var sold = document.getElementsByName("sold").item(0).value;
	if (!validate_required(sold)){
		document.getElementById("soldErrorMsg").innerHTML=" Sold is required!";
		result =  false;
	}else if (!validate_credit_card_format(sold)){
		document.getElementById("soldErrorMsg").innerHTML=" Sold Format incorrect!";
		result =  false;
	}else if (parseFloat(sold) < parseFloat(0)){
		document.getElementById("soldErrorMsg").innerHTML=" Sold must > 0 !";
		result =  false;
	}else if (parseFloat(sold) > parseFloat(soldMax)){
		document.getElementById("SoldErrorMsg").innerHTML=" Sold over limit " + soldMax + " !";
		result =  false;
	}
	
	if (result)
		return true;
	else
		return false;
}